package newmeeting;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class AppointmentListPanel extends JPanel implements ListSelectionListener, ActionListener{
	
	private DefaultListModel listModel;
	private AppointmentPanel appointmentPanel;
	private JList appointmentJList;
	private JScrollPane scrollPanel;
	private JButton createAppointmentButton;
	private InvitesListPanel invitesListPanel;
	
	public static void main(String args[]) {
		JFrame frame = new JFrame("Appointments");
		AppointmentListPanel panel = new AppointmentListPanel();
		DefaultListModel model = panel.getModel();

		model.addElement(new Appointment("Funtime", "15.04.11", "10:30", "11:30", Room.funroom, "let's go all out peeps!"));
		model.addElement(new Appointment("Lametime", "15.04.11", "11:30", "12:30", Room.lameroom, "let's work our asses off peeps!"));
		model.addElement(new Appointment("Lunchmeeting", "15.04.11", "12:30", "13:00", Room.funroom, "nomnomnom"));
		
		frame.getContentPane().add(new InvitesListPanel());
		frame.getContentPane().add(new AppointmentPanel());
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public AppointmentListPanel() {
		super();
		listModel = new DefaultListModel();
		GridLayout gridLayout = new GridLayout(2, 3);
		setLayout(gridLayout);

		appointmentJList = new JList(getModel());
		appointmentJList.setName("AppointmentList");
		appointmentJList.addListSelectionListener(this);
		appointmentJList.setCellRenderer(new AppointmentRenderer());
		appointmentJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPanel = new JScrollPane(appointmentJList);
		scrollPanel.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		appointmentPanel = new AppointmentPanel();
		appointmentPanel.setName("AppointmentPanel");
		invitesListPanel = new InvitesListPanel();
		invitesListPanel.setName("InvitesListPanel");
		createAppointmentButton = new JButton();
		createAppointmentButton.setName("CreateAppointmentButton");
		createAppointmentButton.setText("Create");
		createAppointmentButton.addActionListener(this);
		add(appointmentPanel);
		add(invitesListPanel);
		add(scrollPanel);
		add(createAppointmentButton);
		setModel(listModel);
	}
	
	public void setModel(DefaultListModel lModel){
		this.listModel = lModel;
		appointmentJList.setModel(lModel);
	}
	
	public DefaultListModel getModel(){
		return this.listModel;
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		Appointment selectedPerson = (Appointment) appointmentJList.getSelectedValue();
		appointmentPanel.setModel(selectedPerson);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(createAppointmentButton)) {
			if (appointmentJList.getSelectedValue() != null) {
				Appointment newAppointment = new Appointment(	appointmentPanel.getModel().getTitle(),
															appointmentPanel.getModel().getDate(), 
															appointmentPanel.getModel().getTimeStart(), 
															appointmentPanel.getModel().getTimeEnd(), 
															appointmentPanel.getModel().getRoom(),
															appointmentPanel.getModel().getDescription());
				listModel.add(0, newAppointment);
				appointmentJList.setSelectedIndex(0);
			}
			else if (appointmentJList.getSelectedValue() == null) {
				Appointment newAppointment = new Appointment("title of appointment", "dd.mm.yy", "hh:mm", "hh:mm", Room.occupiedroom, "what's goin' down dawg?");
				listModel.add(0, newAppointment);
				appointmentJList.setSelectedIndex(0);
			}
		}
	}
}
